package com.cwj.springboot_study.service.impl;

import com.cwj.springboot_study.model.Dept;
import com.cwj.springboot_study.model.Person;
import com.cwj.springboot_study.service.DeptService;
import com.cwj.springboot_study.service.PersonService;
import com.cwj.springboot_study.service.TransactionalTestService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author wujie.chen
 * 2023/3/7 11:28
 */
@Service
@Slf4j
public class TransactionalTestServiceImpl implements TransactionalTestService {
    @Autowired
    private DeptService deptService;
    @Autowired
    private PersonService personService;
    @Autowired
    private TransactionalTestService transactionalTestService;

    @Override
    public void testTryCatch() {
        transactionalTestService.updateName(40);
        try {
            transactionalTestService.updateName(41);
        }catch (Exception e){
            log.error("update error");
        }
    }
    @Override
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public void updateName(Integer id){
        Person byId = personService.getById(id);
        byId.setUsername(byId.getUsername()+ "_update");
        personService.updateById(byId);
        log.info("nickName.length: {}", byId.getNickName().length());
    }


}
